//
//  HomePageSearchViewController.m
//  JinMaJiaDemo
//
//  Created by GripMeMory on 15/10/16.
//  Copyright © 2015年 GripMeMory. All rights reserved.
//

#import "HomePageSearchViewController.h"
#import "UIView+Sizes.h"

#define PLISTFILENAME @"Property List.plist"
#define PATH  [[NSBundle mainBundle]pathForResource:PLISTFILENAME ofType:nil]

#define kScreenWidth [UIScreen mainScreen].bounds.size.width

#define kSearchBarWidth 200
#define kSearchBarLeftMargin 10

@interface HomePageSearchViewController () <UISearchBarDelegate, UISearchDisplayDelegate>

{
    UISearchBar *searchbar;
    NSMutableArray *searchHistory;
    NSMutableArray *labelArr;
    
    UITextField *textField;
}

@end

@implementation HomePageSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [searchbar resignFirstResponder];
    [self.navigationController popToRootViewControllerAnimated:NO];
}

- (void)customNavBar{
    searchbar = [[UISearchBar alloc] initWithFrame:CGRectMake(kSearchBarLeftMargin, 0, kSearchBarWidth, 30)];
    searchbar.showsCancelButton = YES;
//    searchbar.showsBookmarkButton = YES;
    searchbar.delegate = self;
    searchbar.placeholder = @"搜索";
    [[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitle:@"取  消"];
    self.navigationItem.titleView = searchbar;
}

- (void)customMainView{
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self refreshView];
}

- (void)refreshView{
    [self.view removeAllSubviews];
    [labelArr removeAllObjects];
    
    textField = [[UITextField alloc] initWithFrame:CGRectMake(self.view.centerX, self.view.centerY, 100, 30)];
    [textField addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    textField.borderStyle = UITextBorderStyleBezel;
    textField.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:textField];

    
    NSArray *plistArr = [[NSArray arrayWithContentsOfFile:PATH] objectAtIndex:1];
    for (int i  = 0; i < plistArr.count; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10 + (i % 4) * (kScreenWidth / 4 - 10 + 5), 100 + (i / 4) * 35, kScreenWidth / 4 - 10, 30)];
        label.text = plistArr[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.clipsToBounds = YES;
        label.backgroundColor = [UIColor lightGrayColor];
        label.layer.borderColor = [[UIColor redColor] CGColor];
        label.layer.cornerRadius = 10;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(LabelClick:)];
        [label addGestureRecognizer:tap];
        label.userInteractionEnabled = YES;
        
        [labelArr addObject:label];
    }
    
    for (UILabel *label in labelArr) {
        [self.view addSubview:label];
    }
}

- (void)creatUI{
    searchHistory = [NSMutableArray arrayWithCapacity:10];
    labelArr = [NSMutableArray arrayWithCapacity:10];
    [self customNavBar];
    [self customMainView];
}

- (void)textFieldEditChanged:(UITextField *)textfield{
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[\u4e00-\u9fa5]" options:NSRegularExpressionCaseInsensitive error:&error];
    if ([regex matchesInString:textField.text options:0 range:NSMakeRange(0, textField.text.length)].count > 0) {
        
    }else{
        
    }
}

#pragma mark - Label Click

- (void)LabelClick:(UITapGestureRecognizer *)tap{
    if ([tap.view isKindOfClass:[UILabel class]]) {
        UILabel *label = (UILabel *)tap.view;
        searchbar.text = label.text;
        [searchbar becomeFirstResponder];
    }
}

#pragma mark - SearchBar Delegate
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
    searchHistory = (NSMutableArray *)[[NSMutableArray arrayWithContentsOfFile:PATH] objectAtIndex:1];
    
    [searchBar resignFirstResponder];
    for (NSString *string in searchHistory) {
        if ([string isEqualToString:searchBar.text]) {
            [searchHistory removeObject:string];
            break;
        }
    }
    
    [searchHistory insertObject:searchBar.text atIndex:0];
    NSMutableArray *wholeArr = [NSMutableArray arrayWithContentsOfFile:PATH];
    [wholeArr replaceObjectAtIndex:1 withObject:searchHistory];
    [wholeArr writeToFile:PATH atomically:YES];

    [self refreshView];
}

@end
